<?php
namespace App\Admin\Controllers;

/**
 * @Author: jack
 * @Date:   2022-02-14 19:53:00
 * @Last Modified by:   jack
 * @Last Modified time: 2023-12-28 15:18:41
 */


use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\Models\Config as MConfig;
use App\Models\User;
use App\Models\UserBingshi;
use App\Models\UserDangan;
use App\Models\UserWenjuan;
use App\Admin\Repositories\Shop;
use Dcat\Admin\Admin;
class ApiController extends Controller
{
    public function index($action = '', Request $request)
    {
        if(method_exists($this, $action)) {
            return $this->$action($request);
        }
        return [];
    }

    public function configOptions($request){
    	$pid = $request->get('q');
    	return MConfig::where('pid', $pid)->get(['id', \DB::raw('name as text')]);
    }
    
    public function user($request){
        $q = $request->get('q');
        return User::where(function($query) use ($q){
            $query->OrWhere('name', 'like', "%$q%")->OrWhere('mobile', 'like', "%$q%");
        })->paginate(10, ['id', \DB::raw("CONCAT(name, '[',mobile ,']') as text "), 'name', 'mobile', 'school', 'class_name', 'sex', 'birthdate', 'address']);
    }

    public function userAutocomplete($request){
        $countries = User::when(request('query'), function ($query, $value) {
            $query->select('id', \DB::raw("CONCAT(name, '[',mobile ,']') as text "), 'name',  'school', 'class_name', 'sex', 'birthdate', 'address')->where('name', 'like', "%{$value}%")->OrWhere('mobile', 'like', "%$value%");
        })->get();

        return Admin::json($countries->toArray());
    }


    public function changeShop($request){
        return Shop::changesShop($request->id);
    }

    public function saveUser($request){
        $user =  User::where('id', $request->id)->first();
        if(!$user) $user = new User();
        $data = $request->only('name','mobile', 'sex', 'school', 'class_name', 'birthdate', 'address', 'shop_id');
        $data['birthdate'] = date('Y-m-d', strtotime($data['birthdate']));
        foreach ($data as $key => $value) {
            $user->$key = $value;
        }
        $user->save();
        // dd($user);
        return ['code'=> 1,'message'=>'保存成功', 'user'=>$user];
    }



    // public function saveBingshi($request){
    //     $user = $request->user;
    //     $mobile = $request->input('user.mobile');
    //     if(strstr($mobile, '[')) {
    //         if (preg_match_all('/\[(\d{11})\]/', $mobile, $matches)) {
    //             $mobile = trim($matches[1][0]);
    //             $user = User::where('mobile', $mobile)->first();
    //             if(!$user) {
    //                 $user = new User();
    //                 $user->mobile = $mobile;
    //             }
    //         }else{
    //             $user = new User();
    //             $user->mobile = $mobile;
    //         }
    //     }else{
    //         $user = new User();
    //         $user->mobile = $mobile;
    //     }
    //     $user->name = $request->input('user.name');
    //     $user->sex = $request->input('user.sex');
    //     $user->school = $request->input('user.school');
    //     $user->class_name = $request->input('user.class_name');
    //     $user->birthdate = $request->input('user.birthdate');
    //     $user->address = $request->input('user.address');
    //     $user->shop_id = $request->input('user.shop_id');
    //     $user->save();

    //     //保存病史
    //     $bingshi = new UserBingshi();
    //     $bingshi->user_id = $user->id;
    //     $bingshi->zhusu = $request->input('bingshi.zhusu');
    //     $bingshi->daijing = $request->input('bingshi.daijing');
    //     $bingshi->xiguan = $request->input('bingshi.xiguan');
    //     $bingshi->kuangjia_date = $request->input('bingshi.kuangjia_date');
    //     $bingshi->xianyou_date = $request->input('bingshi.xianyou_date');
    //     $bingshi->quguang_dpt = $request->input('bingshi.quguang_dpt');
    //     $bingshi->jiaomo_date = $request->input('bingshi.jiaomo_date');
    //     $bingshi->tingdai_date = $request->input('bingshi.tingdai_date');
    //     $bingshi->other = $request->input('bingshi.other');
    //     $bingshi->jiwangshi = $request->input('bingshi.jiwangshi');
    //     $bingshi->jiwangshi_other = $request->input('bingshi.jiwangshi_other');
    //     $bingshi->fuqin = $request->input('bingshi.fuqin');
    //     $bingshi->muqin = $request->input('bingshi.muqin');
    //     $bingshi->jiazu = $request->input('bingshi.jiazu');
    //     $bingshi->chusheng = $request->input('bingshi.chusheng');
    //     $bingshi->save();
    //     return ['code'=> 1,'message'=>'保存成功'];
    // }


    public function saveDang($request){
        $dangan = $request->input('dangan');
        if(!$dangan['user_id']) {
            return ['code'=>0,'message'=>'请先保存基本信息'];
        }
        $UserDangan = new UserDangan();
        
        foreach ($dangan as $key => $value) {
            if(in_array($key, ['_previous_', 'yanzhu_right_1_1', 'yanzhu_right_1', 'yanzhu_left_1_1', 'yanzhu_left_1']))continue;      
            $UserDangan->$key = $value;
        }
        $UserDangan->save();

        $wenjuan = $request->input('wenjuan');

        $UserWenjuan = new UserWenjuan();
        foreach ($wenjuan as $key => $value) {
            if(in_array($key, [ '_previous_']))continue;
            $UserWenjuan->$key = $value;
        }
        $UserWenjuan->user_id = $dangan['user_id'];
        $UserWenjuan->save();
        return ['code'=> 1,'message'=>'保存成功'];
    }


    public function getAllData($request){
        $user = User::with(['bingshis', 'dangans'])->where('id', $request->user_id)->first();
        return ['user'=> $user,'message'=>'保存成功'];
    }

    public function saveBingshi($request){
        $bingshi = new UserBingshi();
        $bingshi->user_id = $request->user_id;
        $bingshi->zhusu = $request->zhusu;
        $bingshi->daijing = $request->daijing;
        $bingshi->xiguan = $request->xiguan;
        $bingshi->kuangjia_date = $request->kuangjia_date;
        $bingshi->xianyou_date = $request->xianyou_date;
        $bingshi->quguang_dpt = $request->quguang_dpt;
        $bingshi->jiaomo_date = $request->jiaomo_date;
        $bingshi->tingdai_date = $request->tingdai_date;
        $bingshi->other = $request->other;
        $bingshi->jiwangshi = $request->jiwangshi;
        $bingshi->jiwangshi_other = $request->jiwangshi_other;
        $bingshi->fuqin = $request->fuqin;
        $bingshi->muqin = $request->muqin;
        $bingshi->jiazu = $request->jiazu;
        $bingshi->chusheng = $request->chusheng;
        $bingshi->save();
        return ['code'=> 1,'message'=>'保存成功'];
    }

    public function saveDangan($request){
        $dangan = new UserDangan();
        if($request->id && $request->type == 0) $dangan = UserDangan::find($request->id);

        $data = $request->all();
        if($request->type ==1) {
            unset($data['id']);
            unset($data['wj']['id']);
        }
        $wj = $data['wj'];
        unset($data['wj']);
        unset($data['type']);
        $data['yanzhu_left'] = $data['yanzhu_left'] . '.'. $data['yanzhu_left_100'];
        $data['yanzhu_right'] = $data['yanzhu_right']  . '.'. $data['yanzhu_right_100'];
        unset($data['yanzhu_left_100']);
        unset($data['yanzhu_right_100']);
        foreach ($data as $key => $value) {
            $dangan->$key = $value;
        }
        $dangan->write_date = date('Y-m-d', strtotime($dangan->write_date));
        $dangan->save();

        $wj['dangan_id'] = $dangan->id;
        $wj['user_id'] = $dangan->user_id;
        if(isset($wj['id'])) {
            $wenjuan = UserWenjuan::find($wj['id']);
        }else{
            $wenjuan = new UserWenjuan();
        }
        foreach ($wj as $key => $value) {
            $wenjuan->$key = $value;
        }
        $wenjuan->save();
        return ['code'=> 1,'message'=>'保存成功'];

    }
    
}